package ru.vasilij.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;

import ru.vasilij.selenium.model.Film;

public class InternalPage extends AnyPage {
	
	public InternalPage(PageManager pages) {
		super(pages);
	}
	
	public InternalPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector("nav")));
		return this;
	}
  
	@FindBy(css = "nav a[href $= '?go=profile']")
	private WebElement userProfileLink;

	@FindBy(css = "nav a[href $= '?go=users']")
	private WebElement userManagementLink;

	@FindBy(css = "nav a[href $= '?logout']")
	private WebElement logoutLink;
	
	private WebElement addFilmLink(){
		return driver.findElement(By.cssSelector("nav a[href $= '?go=add']"));
	}
	
	public FilmAddPage clickAddFilmPage() {
		addFilmLink().click();
		return pages.filmAddPage;
	}
  
	public UserProfilePage clickUserProfilePage() {
		userProfileLink.click();
		return pages.userProfilePage;
    }

	public UserManagementPage clickUserManagementLink() {
		userManagementLink.click();
		return pages.userManagementPage;
	}
  
	public LoginPage clickLogoutLink() {
		logoutLink.click();
		wait.until(alertIsPresent()).accept();
		return pages.loginPage;
	}

	public FilmPage clickFirstFilm() {
		driver.findElement(By.xpath("//div[@id='results']/a[1]")).click();
		return pages.filmPage;
	}

	public InternalPage waitLoadBlockFilms() {
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#results")));
		return pages.internalPage;
	}

	public List<Film> searchFilm(String title) throws InterruptedException {
		List<Film> LF = new ArrayList<Film>();
		WebElement eSearchInput = driver.findElement(By.id("q"));
		vClearSearchInput(eSearchInput);
		eSearchInput.sendKeys(title + Keys.RETURN);
		
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#loading")));
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#results")));
		
		if(driver.findElements(By.cssSelector("div#results div.content")).size() > 0)
			return LF;
		
		List<WebElement> LWE = driver.findElements(By.xpath("//div[@class='movie_box']"));
		for(WebElement WE : LWE) {
			LF.add(new Film().setTitle(WE.findElement(By.xpath("//div[@class='title']")).getText()));
		}
		return LF;
	}
	
	public void clearSearchField() throws InterruptedException {
		WebElement eSearchInput = driver.findElement(By.id("q"));
		vClearSearchInput(eSearchInput);
		eSearchInput.sendKeys(Keys.RETURN);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#loading")));
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#results")));
	}
	
	private void vClearSearchInput(WebElement WE) throws InterruptedException{
		WE.sendKeys(Keys.CONTROL + "a");
		Thread.sleep(200);
		WE.sendKeys(Keys.DELETE);
	}
	
}
